package com.itstyle.seckill.aop;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;

@Aspect
@Component
public class LogAspect {

    private final static Logger logger = LoggerFactory.getLogger(LogAspect.class);

    @Pointcut(value = "@annotation(com.itstyle.seckill.aop.Log)")
    public void logAnnotation() {}


    @Around("logAnnotation()")
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        MethodSignature methodSignature = (MethodSignature) proceedingJoinPoint.getSignature();
        Method method = methodSignature.getMethod();
        Log log = method.getAnnotation(Log.class);
        String value = log.value();
        String description = log.description();
        LogType[] LogTypes = log.type();
        String logTypeName = null;
        if (LogTypes.length > 0) {
            logTypeName = LogTypes[0].name();
        }
        logger.info("执行的操作: {}, 描述: {}, 类型: {}", value, description, logTypeName);
        return proceedingJoinPoint.proceed();
    }
}